package cc.bailongma.me.fragment;


import java.util.ArrayList;
import java.util.List;

import cc.bailongma.me.R;
import cc.bailongma.me.adapter.ParentListAdapter;
import cc.bailongma.me.entity.Parent;
import cc.bailongma.me.view.AddParentDialog;
import cc.bailongma.me.view.AddParentDialog.MyDialogListener;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class ParentNumberFragment extends Fragment implements OnClickListener,MyDialogListener{
	public static final String TAG="ParentNumberFragment";
	private List<Parent> parents;
	private ListView lv;
	private RelativeLayout layout_empty;
	private ParentListAdapter parentListAdapter;
	public  TextView tv_add_count;
	public  TextView tv_total;
	public ParentNumberFragment(){
	}

	@SuppressWarnings("unchecked")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Bundle bundler=this.getArguments();
		
		parents=(List<Parent>) bundler.getSerializable("parents");
		View view= inflater.inflate(R.layout.parentnumberfragment,container,false);
		lv=(ListView) view.findViewById(R.id.listView1);
		
		tv_add_count=(TextView) view.findViewById(R.id.tv_add_count);
		tv_total=(TextView) view.findViewById(R.id.tv_total);
		
		
		layout_empty=(RelativeLayout) view.findViewById(R.id.layout_impty);
		view.findViewById(R.id.tv_add_parent).setOnClickListener(this);
		view.findViewById(R.id.tv_add).setOnClickListener(this);
		
		parentListAdapter=new ParentListAdapter(getActivity(), parents,this);
		lv.setAdapter(parentListAdapter);
		
		if(parents.size()==0){
			layout_empty.setVisibility(View.VISIBLE);
		}else{
			layout_empty.setVisibility(View.GONE);
		}

		return view;
	}
	public void updateUI(){
		if(parents.size()==0){
			layout_empty.setVisibility(View.VISIBLE);
		}else{
			layout_empty.setVisibility(View.GONE);
		}
		tv_add_count.setText(parents.size()+"");
		tv_total.setText("个，还可以添加"+(20-parents.size()+"个"));
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_add_parent:
		case R.id.tv_add:
			addParent();
			break;
		}
		
	}

	private void addParent() {
		AddParentDialog dialog=new AddParentDialog(getActivity(), this);
		dialog.show();
		dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); 
		dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
	}
	
	@Override
	public void onOkClick(Parent parent) {
		if(parents==null){
			parents=new ArrayList<Parent>();
		}
		parents.add(parent);
		update();
	}

	public void update() {
		updateUI();
		parentListAdapter.notifyDataSetChanged();
		
	}
}
